package servlet;

import dao.OrderDao;
import pojo.Order;
import pojo.QueryOrder;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet({"/orderSkip", "/orderAll", "/orderQuery", "/orderPgUp", "/orderPgDn"})
public class OrderManage extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        String servletPath = request.getServletPath();
        if ("/orderSkip".equals(servletPath)) {
            doSkip(request, response);
        } else if ("/orderAll".equals(servletPath)) {
            doAll(request, response);
        } else if ("/orderQuery".equals(servletPath)) {
            doQuery(request, response);
        } else if ("/orderPgUp".equals(servletPath)) {
            doPgUp(request, response);
        } else if ("/orderPgDn".equals(servletPath)) {
            doPgDn(request, response);
        }
    }

    private void doSkip(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if (user.getAccount().equals("root")) {
            HashMap<String, String> map = new HashMap<>();

            map.put("name", "");
            map.put("account", "");
            map.put("status", "-1");
            map.put("year", "");
            map.put("month", "");
            map.put("day", "");

            List<QueryOrder> orderList = new OrderDao().findOrder(1, 10, "", "", -1, -1, -1, -1);
            int size = new OrderDao().countOrder(null, null, -1, -1, -1, -1);
            session.setAttribute("orderList", orderList);
            session.setAttribute("map", map);
            session.setAttribute("active", 1);
            session.setAttribute("size", size);
            request.getRequestDispatcher("administrator.jsp").forward(request, response);
        } else {
            HashMap<String, String> map = new HashMap<>();

            map.put("name", "");
            map.put("status", "-1");
            map.put("year", "");
            map.put("month", "");
            map.put("day", "");

            List<QueryOrder> orderList = new OrderDao().findOrder(1, 10, "", user.getAccount(), -1, -1, -1, -1);
            int size = new OrderDao().countOrder(null, user.getAccount(), -1, -1, -1, -1);
            session.setAttribute("orderList", orderList);
            session.setAttribute("map", map);
            session.setAttribute("active", 1);
            session.setAttribute("size", size);
            request.getRequestDispatcher("myOrder.jsp").forward(request, response);
        }
    }

    private void doAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        HashMap<String, String> map = new HashMap<>();

        map.put("name", "");
        map.put("account", "");
        map.put("status", "-1");
        map.put("year", "");
        map.put("month", "");
        map.put("day", "");

        List<QueryOrder> orderList = new OrderDao().findOrder(1, 10, null, null, -1, -1, -1, -1);
        int size = new OrderDao().countOrder(null, null, -1, -1, -1, -1);
        session.setAttribute("orderList", orderList);
        session.setAttribute("active", 1);
        session.setAttribute("size", size);
        session.setAttribute("map", map);
        request.getRequestDispatcher("administrator.jsp").forward(request, response);
    }

    private void doQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        HashMap<String, String> map = new HashMap<>();

        String account = request.getParameter("account");
        String course = request.getParameter("course");
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        String day = request.getParameter("day");
        String status = request.getParameter("status");
        int page = Integer.parseInt(request.getParameter("page"));

        map.put("name", course);
        map.put("account", account);
        map.put("status", status);
        map.put("year", year);
        map.put("month", month);
        map.put("day", day);

        if (year == null || year.equals("")) {
            year = "-1";
        }
        if (month == null || month.equals("")) {
            month = "-1";
        }
        if (day == null || day.equals("")) {
            day = "-1";
        }

        List<QueryOrder> orderList = new OrderDao().findOrder(page, 10, course, account, Integer.parseInt(status),
                Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        int size = new OrderDao().countOrder(course, account, Integer.parseInt(status),
                Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        session.setAttribute("orderList", orderList);
        session.setAttribute("active", page);
        session.setAttribute("size", size);
        session.setAttribute("map", map);
        request.getRequestDispatcher("administrator.jsp").forward(request, response);
    }

    private void doPgUp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();

        HashMap<String, String> map = (HashMap<String, String>) session.getAttribute("map");
        int active = (int) session.getAttribute("active");
        int page = 1;
        if (active > 1) {
            page = active - 1;
        } else {
            page = active;
        }

        String year = map.get("year");
        String month = map.get("month");
        String day = map.get("day");

        if (year == null || year.equals("")) {
            year = "-1";
        }
        if (month == null || month.equals("")) {
            month = "-1";
        }
        if (day == null || day.equals("")) {
            day = "-1";
        }

        List<QueryOrder> orderList = new OrderDao().findOrder(page, 10, map.get("name"), map.get("account"),
                Integer.parseInt(map.get("status")), Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        session.setAttribute("orderList", orderList);
        session.setAttribute("active", page);
        request.getRequestDispatcher("administrator.jsp").forward(request, response);
    }

    private void doPgDn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();

        HashMap<String, String> map = (HashMap<String, String>) session.getAttribute("map");
        int size = (int) session.getAttribute("size");
        int active = (int) session.getAttribute("active");
        int pages = ((size - 1) / 10) + 1;
        int page = 1;
        if (active < pages) {
            page = active + 1;
        } else {
            page = active;
        }

        String year = map.get("year");
        String month = map.get("month");
        String day = map.get("day");

        if (year == null || year.equals("")) {
            year = "-1";
        }
        if (month == null || month.equals("")) {
            month = "-1";
        }
        if (day == null || day.equals("")) {
            day = "-1";
        }

        List<QueryOrder> orderList = new OrderDao().findOrder(page, 10, map.get("name"), map.get("account"),
                Integer.parseInt(map.get("status")), Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        session.setAttribute("orderList", orderList);
        session.setAttribute("active", page);
        request.getRequestDispatcher("administrator.jsp").forward(request, response);
    }
}
